#!/usr/bin/env bash

# mkdir -p /nat-ddns

fn_download(){
    # github 下载链接
    # wget https://github.com/fatedier/frp/releases/download/v0.34.0/frp_0.34.0_linux_amd64.tar.gz

    # gitee 下载链接
    wget https://gitee.com/buyili/nat-ddns/attach_files/486477/download/frp_0.34.0_linux_amd64.tar.gz
}

# cp frp_0.34.0_linux_amd64.tar.gz /nat-ddns

# cd /nat-ddns
# tar zxvf frp_0.34.0_linux_amd64.tar.gz
# mv frp_0.34.0_linux_amd64 frp


# cd frp

# echo '[common]
# bind_port = 7000
# vhost_http_port = 7001' > ./frps.ini

# echo '
# cd /nat-ddns/frp
# nohup ./frps -c ./frps.ini > frps.log 2>&1 &
# echo "$!" > frps.pid
# ' > ./boot.sh && chmod +x ./boot.sh

# cp ./boot.sh /usr/bin/frps

bootDir=$(pwd)


fn_install_online(){
    mkdir -p /nat-ddns
    wget https://gitee.com/buyili/nat-ddns/attach_files/486477/download/frp_0.34.0_linux_amd64.tar.gz -P /nat-ddns
    fn_install
}

fn_install_local(){
    mkdir -p /nat-ddns
    cp frp_0.34.0_linux_amd64.tar.gz /nat-ddns
    fn_install
}

fn_install(){
    cd /nat-ddns
    tar zxvf frp_0.34.0_linux_amd64.tar.gz
    mv frp_0.34.0_linux_amd64 frp
    cd frp

    read -p "Please input vhost_http_port(7001): " vhost_http_port
    if [ -z "$vhost_http_port" ];then
        vhost_http_port="7001"
    fi

#todo 换行问题
    iniContent="[common]\nbind_port = 7000\nvhost_http_port = $vhost_http_port"

    echo -e "\t" "frps.ini:"
    echo -e $iniContent
    

    echo -e $iniContent > ./frps.ini

    rm -rf /usr/bin/frps
    cp $bootDir/frps.sh /usr/bin/frps
    chmod +777 /usr/bin/frps
}

case $1 in
    online)           fn_install_online        ;;
    *)              fn_install_local        ;;
esac


